﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace PokerClient
{

    public partial class FormGame : Form, PokerClient.ServiceReference1.IGameCallback
    {
        private int seatNR;
        private PokerClient.ServiceReference1.GameClient proxy;
        FormGame formGame;
        int tableID;
        string UserName;
        int coundown = 3;
        Button b1 = new Button();//Card1
        Button b2 = new Button();//cacrd2
        public FormGame(int tableId, string userName)
        {
            InitializeComponent();
            InstanceContext context = new InstanceContext(this);
            proxy = new PokerClient.ServiceReference1.GameClient(context);
            tableID = tableId;
            UserName = userName;
            toolStripLabel1.Text = "Username: " + userName;
            updateTakeSeat();
            //giveCards(1, 2, "♠", 3, "♠");
        }

        public void notifyOtherPlayers(string username, string action)
        {
            toolStripLabel2.Text = "Notification   username: " + username + " action: " + action;
        }

        public void updatePlayer(string username, int money, int seatNr, bool turn, string action)
        {
            switch (seatNr)
            {
                case 1:
                    button1.Enabled = false;
                    button1.Text = username + "\n Money: " + money + "\n " + action;
                    if (turn)
                        button1.Text += "\n players turn";
                    break;
                case 2:
                    button2.Enabled = false;
                    button2.Text = username + "\n Money: " + money + "\n " + action;
                    if (turn)
                        button2.Text += "\n players turn";
                    break;
                case 3:
                    button3.Enabled = false;
                    button3.Text = username + "\n Money: " + money + "\n " + action;
                    if (turn)
                        button3.Text += "\n players turn";
                    break;
                case 4:
                    button4.Enabled = false;
                    button4.Text = username + "\n Money: " + money + "\n " + action;
                    if (turn)
                        button4.Text += "\n players turn";
                    break;
                case 5:
                    button5.Enabled = false;
                    button5.Text = username + "\n Money: " + money + "\n " + action;
                    if (turn)
                        button5.Text += "\n players turn";
                    break;
                case 6:
                    button6.Enabled = false;
                    button6.Text = username + "\n Money: " + money + "\n " + action;
                    if (turn)
                        button6.Text += "\n players turn";
                    break;
                case 7:
                    button7.Enabled = false;
                    button7.Text = username + "\n Money: " + money + "\n " + action;
                    if (turn)
                        button7.Text += "\n players turn";
                    break;
                case 8:
                    button8.Enabled = false;
                    button8.Text = username + "\n Money: " + money + "\n " + action;
                    if (turn)
                        button8.Text += "\n players turn";
                    break;
                default:
                    break;
            }
        }

        public void updateCards(string card, int turn)
        {
            switch (turn)
            {
                case 1:
                    button9.Visible = true;
                    button9.Text = card;
                    break;
                case 2:
                    button10.Visible = true;
                    button10.Text = card;
                    break;
                case 3:
                    button11.Visible = true;
                    button11.Text = card;
                    break;
                case 4:
                    button12.Visible = true;
                    button12.Text = card;
                    break;
                case 5:
                    button13.Visible = true;
                    button13.Text = card;
                    break;
                case 6:
                    button9.Visible = false;
                    button10.Visible = false;
                    button11.Visible = false;
                    button12.Visible = false;
                    button13.Visible = false;
                    break;
            }
        }

        public void updatePot(int amount)
        {
            label2.Text = "Pot: " + amount.ToString();
        }

        public void updateButtons(int posibilities, int minRaiseAmount)
        {
            switch (posibilities)
            {
                case 1:
                    btnCall.Enabled = false;
                    btnCheck.Enabled = true;
                    btnFold.Enabled = true;
                    btnRaise.Enabled = true;
                    numericUpDown1.Minimum = minRaiseAmount;
                    break;
                case 2:
                    btnCall.Enabled = true;
                    btnCheck.Enabled = false;
                    btnFold.Enabled = true;
                    btnRaise.Enabled = true;
                    numericUpDown1.Minimum = minRaiseAmount;
                    break;
            }
        }

        public void gameStarts(string message)
        {
            label1.Text = message;
            timer1.Start();
        }

        public void giveCards(int seatNr, string card1, string card2)
        {

            switch (seatNr)
            {
                case 1:
                    b1.Text = card1;
                    b2.Text = card2;
                    break;
                case 2:
                    b1.Text = card1;
                    b2.Text = card2;
                    break;
                case 3:
                    b1.Text = card1;
                    b2.Text = card2;
                    break;
                case 4:
                    b1.Text = card1;
                    b2.Text = card2;
                    break;
                case 5:
                    b1.Text = card1;
                    b2.Text = card2;
                    break;
                case 6:
                    b1.Text = card1;
                    b2.Text = card2;
                    break;
                case 7:
                    b1.Text = card1;
                    b2.Text = card2;
                    break;
                case 8:
                    b1.Text = card1;
                    b2.Text = card2;
                    break;
                default:
                    break;
            }
            //btnCheck.Enabled = true;
            //btnCall.Enabled = true;
            //btnFold.Enabled = true;
        }

        public void anounceWinner(int seatNr)
        {
            switch (seatNr)
            {
                case 1:
                    button1.Text += "\n winner";
                    break;
                case 2:
                    button2.Text += "\n winner";
                    break;
                case 3:
                    button3.Text += "\n winner";
                    break;
                case 4:
                    button4.Text += "\n winner";
                    break;
                case 5:
                    button5.Text += "\n winner";
                    break;
                case 6:
                    button6.Text += "\n winner";
                    break;
                case 7:
                    button7.Text += "\n winner";
                    break;
                case 8:
                    button8.Text += "\n winner";
                    break;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            proxy.TakeSeat(tableID, 50, UserName, 1);
            label1.Text = "ok";
            updateTakeSeat();
            disableAllTakeSeat();
            createTheButton(1);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            proxy.TakeSeat(tableID, 50, UserName, 2);
            label1.Text = "ok";
            updateTakeSeat();
            disableAllTakeSeat();
            createTheButton(2);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            proxy.TakeSeat(tableID, 50, UserName, 3);
            label1.Text = "ok";
            updateTakeSeat();
            disableAllTakeSeat();
            createTheButton(3);

        }

        private void button4_Click(object sender, EventArgs e)
        {
            proxy.TakeSeat(tableID, 50, UserName, 4);
            label1.Text = "ok";
            updateTakeSeat();
            disableAllTakeSeat();
            createTheButton(4);

        }

        private void button5_Click(object sender, EventArgs e)
        {
            proxy.TakeSeat(tableID, 50, UserName, 5);
            label1.Text = "ok";
            updateTakeSeat();
            disableAllTakeSeat();
            createTheButton(5);

        }

        private void button6_Click(object sender, EventArgs e)
        {
            proxy.TakeSeat(tableID, 50, UserName, 6);
            label1.Text = "ok";
            updateTakeSeat();
            disableAllTakeSeat();
            createTheButton(6);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            proxy.TakeSeat(tableID, 50, UserName, 7);
            label1.Text = "ok";
            updateTakeSeat();
            disableAllTakeSeat();
            createTheButton(7);

        }

        private void button8_Click(object sender, EventArgs e)
        {
            proxy.TakeSeat(tableID, 50, UserName, 8);
            updateTakeSeat();
            disableAllTakeSeat();
            createTheButton(8);
        }

        private void createTheButton(int seatNr)
        {
            b1 = new Button();
            b2 = new Button();
            Point p = new Point();
            switch (seatNr)
            {
                case 1:
                    p = button1.Location;
                    Controls.Add(b1);
                    b1.Location = new Point(p.X + 89, p.Y);
                    b1.Size = new Size(39, 69);
                    Controls.Add(b2);
                    b2.Location = new Point(p.X + 134, p.Y);
                    b2.Size = new Size(39, 69);
                    break;
                case 2:
                    p = button2.Location;
                    Controls.Add(b1);
                    b1.Location = new Point(p.X + 89, p.Y);
                    b1.Size = new Size(39, 69);
                    Controls.Add(b2);
                    b2.Location = new Point(p.X + 134, p.Y);
                    b2.Size = new Size(39, 69);
                    break;
                case 3:
                    p = button3.Location;
                    Controls.Add(b1);
                    b1.Location = new Point(p.X + 89, p.Y);
                    b1.Size = new Size(39, 69);
                    Controls.Add(b2);
                    b2.Location = new Point(p.X + 134, p.Y);

                    break;
                case 4:
                    p = button4.Location;
                    Controls.Add(b1);
                    b1.Location = new Point(p.X + 89, p.Y);
                    b1.Size = new Size(39, 69);
                    Controls.Add(b2);
                    b2.Location = new Point(p.X + 134, p.Y);
                    b2.Size = new Size(39, 69);
                    break;
                case 5:
                    p = button5.Location;
                    Controls.Add(b1);
                    b1.Location = new Point(p.X + 89, p.Y);
                    b1.Size = new Size(39, 69);
                    Controls.Add(b2);
                    b2.Location = new Point(p.X + 134, p.Y);
                    b2.Size = new Size(39, 69);

                    break;
                case 6:
                    p = button6.Location;
                    Controls.Add(b1);
                    b1.Location = new Point(p.X + 89, p.Y);
                    b1.Size = new Size(39, 69);
                    Controls.Add(b2);
                    b2.Location = new Point(p.X + 134, p.Y);
                    b2.Size = new Size(39, 69);
                    break;
                case 7:
                    p = button7.Location;
                    Controls.Add(b1);
                    b1.Location = new Point(p.X + 89, p.Y);
                    b1.Size = new Size(39, 69);
                    Controls.Add(b2);
                    b2.Location = new Point(p.X + 134, p.Y);
                    b2.Size = new Size(39, 69);
                    break;
                case 8:
                    p = button8.Location;
                    Controls.Add(b1);
                    b1.Location = new Point(p.X + 89, p.Y);
                    b1.Size = new Size(39, 69);
                    Controls.Add(b2);
                    b2.Location = new Point(p.X + 134, p.Y);
                    b2.Size = new Size(39, 69);
                    break;
            }
            

        }

        private void updateTakeSeat()
        {
            foreach (var x in proxy.GetListOfPlayers(tableID))
            {
                switch (x.SeatNr)
                {
                    case 1:
                        button1.Enabled = false;
                        button1.Text = x.Username + "\n Money: " + x.MoneyOnTable;
                        if (x.IsYourTurn)
                            button1.Text += "\n players turn";
                        break;
                    case 2:
                        button2.Enabled = false;
                        button2.Text = x.Username + "\n Money: " + x.MoneyOnTable;
                        if (x.IsYourTurn)
                            button2.Text += "\n players turn";
                        break;
                    case 3:
                        button3.Enabled = false;
                        button3.Text = x.Username + "\n Money: " + x.MoneyOnTable;
                        if (x.IsYourTurn)
                            button3.Text += "\n players turn";
                        break;
                    case 4:
                        button4.Enabled = false;
                        button4.Text = x.Username + "\n Money: " + x.MoneyOnTable;
                        if (x.IsYourTurn)
                            button4.Text += "\n players turn";
                        break;
                    case 5:
                        button5.Enabled = false;
                        button5.Text = x.Username + "\n Money: " + x.MoneyOnTable;
                        if (x.IsYourTurn)
                            button5.Text += "\n players turn";
                        break;
                    case 6:
                        button6.Enabled = false;
                        button6.Text = x.Username + "\n Money: " + x.MoneyOnTable;
                        if (x.IsYourTurn)
                            button6.Text += "\n players turn";
                        break;
                    case 7:
                        button7.Enabled = false;
                        button7.Text = x.Username + "\n Money: " + x.MoneyOnTable;
                        if (x.IsYourTurn)
                            button7.Text += "\n players turn";
                        break;
                    case 8:
                        button8.Enabled = false;
                        button8.Text = x.Username + "\n Money: " + x.MoneyOnTable;
                        if (x.IsYourTurn)
                            button8.Text += "\n players turn";
                        break;
                    default:
                        break;
                }

            }
        }

        private void disableAllTakeSeat()
        {
            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button5.Enabled = false;
            button6.Enabled = false;
            button7.Enabled = false;
            button8.Enabled = false;

        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            proxy.check(tableID, UserName);
        }

        private void btnCall_Click(object sender, EventArgs e)
        {
            proxy.call(tableID, UserName);
        }

        private void btnFold_Click(object sender, EventArgs e)
        {
            proxy.fold(tableID, UserName);
        }

        private void btnRaise_Click(object sender, EventArgs e)
        {
            proxy.raise(tableID, UserName, Convert.ToInt32(numericUpDown1.Value));
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            DoSomething();
            coundown--;
        }

        private void DoSomething()
        {
            if (coundown > 0)
            {
                label1.Text = coundown.ToString();
            }
            else
            {
                timer1.Stop();
                label1.Text = "";
                coundown = 3;
            }

        }

    }
}
